package org.miyuki4j.cfgtool;

import org.miyuki4j.cfgtool.exception.CodeGenerateException;
import org.miyuki4j.cfgtool.generator.FreeMarkerCodeGenerator;
import org.miyuki4j.cfgtool.generator.JavaCodeGenerator;

enum CodeGeneratorEnum {

    FREEMARKER {
        @Override
        protected CodeGenerator create() {
            return new FreeMarkerCodeGenerator();
        }
    },

    JAVA {
        @Override
        protected CodeGenerator create() {
            return new JavaCodeGenerator();
        }
    },
    ;

    public static CodeGeneratorEnum of(String type) {
        for (CodeGeneratorEnum value : values()) {
            if (value.name().equalsIgnoreCase(type)) {
                return value;
            }
        }
        return null;
    }

    protected abstract CodeGenerator create();

    public CodeGenerator createGenerator() {
        try {
            CodeGenerator generator = create();
            generator.init();
            return generator;
        } catch (Exception e) {
            throw new CodeGenerateException("type:" + name(), e);
        }
    }
}